/*==========================================================================
 * 
 * AddDataDialog.java
 * 
 * $Author: randallco $
 * $Revision: 1.1 $
 * $Date: 2004/10/16 21:21:01 $
 * $Name:  $
 * 
 * Created on 30-Dec-2003
 * Created by Marcel Palko alias Randallco (randallco@users.sourceforge.net)
 *==========================================================================*/
package net.sourceforge.ehep.gui;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.jface.dialogs.MessageDialog;

import net.sourceforge.ehep.core.EHEP;

/**
 * @author Marcel Palko alias Randallco
 * @author randallco@users.sourceforge.net
 */
public class AddDataDialog extends Dialog {
	private int[] result = null;
	private Shell dialog;
	private int type;
	private Text dataSizeText;
	private Text dataValueText;
	private Combo sizeCombo;

	public AddDataDialog(Shell parent, int style, int type) {
		super(parent, style);
		this.type = type;
	}

	public AddDataDialog(Shell parent, int type) {
		this(parent, SWT.APPLICATION_MODAL, type);
	}

	public int[] open() {
		dialog = new Shell(getParent(), getStyle());
		dialog.setText(getText());
		dialog.setLayout(new GridLayout());
		
		//
		// Panel with data size entry
		//
		Composite sizePanel = new Composite(dialog, SWT.NONE);
		GridLayout sizeGrid = new GridLayout();
		sizeGrid.numColumns = 3;
		sizePanel.setLayout(sizeGrid);
		sizePanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
		Label label = new Label(sizePanel, SWT.NULL);
		label.setText("Amount of data to " + ((type == EHEP.DIALOG_TYPE_INSERT) ? "insert:" : "append:"));
		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dataSizeText = new Text(sizePanel, SWT.SINGLE | SWT.BORDER);
		dataSizeText.setTextLimit(8);
		dataSizeText.setText("1");
		dataSizeText.selectAll();
		dataSizeText.setFocus();
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = 50;
		dataSizeText.setLayoutData(data);
		sizeCombo = new Combo(sizePanel, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
		sizeCombo.add("B");
		sizeCombo.add("kB");
		//sizeCombo.add("MB");
		sizeCombo.select(0);

		//
		// Initial value
		//
		label = new Label(sizePanel, SWT.NULL);
		label.setText("Initial hex value:");
		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dataValueText = new Text(sizePanel, SWT.SINGLE | SWT.BORDER);
		dataValueText.setTextLimit(2);
		dataValueText.setText("00");
		dataValueText.selectAll();

		//
		// Panel with dialog buttons
		//
		Composite buttonPanel = new Composite(dialog, SWT.NONE);
		GridLayout grid = new GridLayout();
		grid.numColumns = 2;
		buttonPanel.setLayout(grid);
		buttonPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		Button ok = new Button(buttonPanel, SWT.PUSH);
		ok.setText("OK");
		data = new GridData();
		data.widthHint = 75;
		ok.setLayoutData(data);
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String sDataSize = dataSizeText.getText().trim();
				String sDataValue = dataValueText.getText().trim();
				int iDataSize = 0;
				int iDataValue = 0;
				
				try {
					iDataSize = Integer.parseInt(sDataSize,10);
				}
				catch (NumberFormatException nfeSize) {
					MessageDialog.openError(getParent().getShell(), EHEP.DIALOG_TITLE_ERROR, "Amount of data: Incorrect number format!");
					dialog.dispose();
					return;
				}
				
				if (iDataSize < 0) {
					MessageDialog.openError(getParent().getShell(), EHEP.DIALOG_TITLE_ERROR, "Amount of data cannot be negative!");
					dialog.dispose();
					return;
				} // if
				
				try {
					iDataValue = Integer.parseInt(sDataValue,16);
				}
				catch (NumberFormatException nfeValue) {
					MessageDialog.openError(getParent().getShell(), EHEP.DIALOG_TITLE_ERROR, "Initial hex value: Incorrect number format!");
					dialog.dispose();
					return;
				}
				
				result = new int[2];
				result[0] = (int) (iDataSize * Math.pow(1024,sizeCombo.getSelectionIndex()));
				result[1] = iDataValue;
				dialog.dispose();
			}
		});
		
		Button cancel = new Button(buttonPanel, SWT.PUSH);
		cancel.setText("Cancel");
		data = new GridData();
		data.widthHint = 75;
		cancel.setLayoutData(data);
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				dialog.dispose();
			}
		});
		
		dialog.setDefaultButton(ok);
		dialog.pack();
		dialog.open();
		
		while (!dialog.isDisposed()) {
			if (!getParent().getDisplay().readAndDispatch()) getParent().getDisplay().sleep();
		}
		
		return result;
	}
}
